
var matchingGame = {};
matchingGame.deck=[
			'cardA','cardA1',
			'cardB','cardB1',
			'cardC','cardC1',
			'cardD','cardD1',
			'cardE','cardE1',
			'cardF','cardF1',
			'cardG','cardG1',
			'cardH','cardH1',
			'cardI','cardI1',
			'cardJ','cardJ1',
			'cardK','cardK1',
			'cardL','cardL1',
			];
matchingGame.i=0;
maudio={};
//音效加载
maudio.cardOnclick=document.getElementById("cardOnclick");
maudio.cardOnclick.volume=.1;//音量
maudio.cardMove=document.getElementById("cardMove");
//maudio.cardDifferent=document.getElementById("cardDifferent");
//maudio.cardDifferent.volume=.9;//音量
maudio.clickInvalid=document.getElementById("clickInvalid");
maudio.youWinning=document.getElementById("youWinning");
maudio.hoverMusic=document.getElementById("hoverMusic");
maudio.youWinning.volume=.4;
var gdata={};
gdata.gname="游客";
gdata.gnum=0;

$(function(){
	//加载数据
	initdata();
	matchingGame.deck.sort(shuffle);
	for(var i=0;i<23;i++){
		$(".card:first-child").clone().appendTo("#cards");
	}
	//初始化牌位置
	$("#cards").children().each(function(index){
		//4*3
		$(this).css({
			"left":($(this).width() + 20) * (index % 8),
			"top":($(this).height() + 20) * Math.floor(index / 8)
		});

		//获取图案
		var pattern= matchingGame.deck.pop();
		//应用纸牌背面图案，并让其可见
		$(this).find(".back").addClass(pattern);
		//吧图案嵌入DOM元素中。
		$(this).attr("data-pattern",pattern);
		//监听牌单机事件
		$(this).click(selectCard);
		$(this).on('mouseover',function(){
			maudio.cardOnclick.play();
		}).on('mouseout',function (){
			maudio.cardOnclick.currentTime=0;
		})
	});
});

function shuffle(){
	return 0.5 - Math.random();
}

function selectCard(){
	matchingGame.i++;
	document.getElementById("number").innerHTML=matchingGame.i;
	//如果已经放开两张
	if ($(".card-filpped").size()==2) {
		nom();
	}
	if ($(".card-filpped").size()>2) {
		return;
	}
	$(this).addClass("card-filpped");
	if ($(".card-filpped").size()==2) {
		checkPattern();
	}
	//一定时间后检测
}

function checkPattern(){
	if (isMatchPattern()) {
	$(".card-filpped").removeClass("card-filpped").addClass("card-removed");
		setTimeout(ism,700);
		maudio.cardMove.play();
	}else{
		//maudio.cardDifferent.play();
	}
}

function isMatchPattern(){
	var cards=$(".card-filpped");
	var pattern=$(cards[0]).data("pattern");
	var anotherPattern=$(cards[1]).data("pattern");

	return (pattern.substring(0,5)==anotherPattern.substring(0,5));
}

function removeTookCards(){
	$(".card-removed").remove();
	var number=$(".card").size();
	if (number==0) {
		maudio.youWinning.play();

		//是否更名
		var nm=prompt("用哪个大名记录这次胜利呢。。。。",gdata.gname);
		if (nm!=null) {
			gdata.gname=nm;
		};
		savedata();
		endgame();
/*		setTimeout(endgame,100);*/
	};
}

function endgame(){
	history.go(0);
}

function ism(){
	$(".card-removed").bind("webkitTransitionEnd",removeTookCards);
}

function nom(){
	$(".card-filpped").removeClass("card-filpped");
}

function times(){}

function savedata(){
	gdata.gnum=matchingGame.i;
	var now=new Date();
	var year=now.getFullYear();
	var month=now.getMonth()+1;
	var day=now.getDate();
	var hours=now.getHours();
	var minutes=now.getMinutes();
	var seconds=now.getSeconds();
	if (minutes<10) {minutes="0" + minutes;};
	if (seconds<10) {seconds="0" + seconds;};
	if (month<10) {month="0" + month;};
	if (day<10) {day="0" + day;};

	var obj={"gname":gdata.gname,
		"gnum":gdata.gnum,
		"gdate":{
				"year":year,
				"month":month,
				"day":day,
				"hours":hours,
				"minutes":minutes,
				"seconds":seconds
			}
		}

	//记录到最近游戏记录中
	localStorage.setItem("gdata1",localStorage.getItem("gdata2"));
	localStorage.setItem("gdata2",localStorage.getItem("gdata3"));
	localStorage.setItem("gdata3",localStorage.getItem("gdata4"));
	localStorage.setItem("gdata4",localStorage.getItem("gdata5"));
	localStorage.setItem("gdata5",JSON.stringify(obj));

	//记录到排行版中。
	if (parseFloat(obj.gnum) < parseFloat((JSON.parse(localStorage.getItem("gdata10"))).gnum)){
		localStorage.setItem("gdata10",JSON.stringify(obj));
	}
	for(var i=10;i >= 7;i--){
		if (parseFloat((JSON.parse(localStorage.getItem("gdata"+i))).gnum) < parseFloat((JSON.parse(localStorage.getItem("gdata"+(i-1)))).gnum)){
			localStorage.setItem("gdata11",JSON.stringify(JSON.parse(localStorage.getItem("gdata"+(i-1)))));
			localStorage.setItem("gdata"+(i-1),JSON.stringify(JSON.parse(localStorage.getItem("gdata"+i))));
			localStorage.setItem("gdata"+i,localStorage.getItem("gdata11"));
		}
	}
}

function initdata(){
	var gdatab=localStorage.getItem("gdata1");
	if (gdatab==null){
		var tempn=prompt("新来的吧^_^~","游客");
		if (tempn!=null) {gdata.gname=tempn;}else{gdata.gname='游客'}
		var now=new Date();
		var year=now.getFullYear();
		var month=now.getMonth()+1;
		var day=now.getDate();
		var hours=now.getHours();
		var minutes=now.getMinutes();
		var seconds=now.getSeconds();
		minutes = (minutes < 10 ? "0" + minutes :minutes);
		seconds = (seconds < 10 ? "0" + seconds :seconds);
		month = (month < 10 ? "0" + month :month);
		day = (day < 10 ? "0" + day :day);

		var objt = {"gname":gdata.gname,
					"gnum":"1000",
					"gdate":{
						"year":year,
						"month":month,
						"day":day,
						"hours":hours,
						"minutes":minutes,
						"seconds":seconds
					}
				}
		for (var i = 1;i<=10; i++){
			localStorage.setItem("gdata"+i,JSON.stringify(objt));
		}
		var obj=JSON.parse(localStorage.getItem("gdata1"));
		for (var i=1;i<=5;i++){
			document.getElementById("cna"+i).innerHTML=obj.gname;
			document.getElementById("cn"+i).innerHTML=obj.gnum;
			document.getElementById("ct"+i).innerHTML=obj.gdate.year+"-"+obj.gdate.month+"-"+obj.gdate.day + "-" + obj.gdate.hours +":"+obj.gdate.minutes +":"+obj.gdate.seconds;

			document.getElementById("hna"+i).innerHTML=obj.gname;
			document.getElementById("hn"+i).innerHTML=obj.gnum;
			document.getElementById("ht"+i).innerHTML=obj.gdate.year+"-"+obj.gdate.month+"-"+obj.gdate.day + "-" + obj.gdate.hours +":"+obj.gdate.minutes +":"+obj.gdate.seconds;
		}
	}else{
		for (var i = 1;i<=5; i++){
			var	objt=localStorage.getItem("gdata"+i);
			obj=JSON.parse(objt);
			document.getElementById("cna"+i).innerHTML=obj.gname;
			document.getElementById("cn"+i).innerHTML=obj.gnum;
			document.getElementById("ct"+i).innerHTML=obj.gdate.year + "-" + obj.gdate.month + "-"+ obj.gdate.day + "-" + obj.gdate.hours + ":" + obj.gdate.minutes + ":" + obj.gdate.seconds;
			var	objt1=localStorage.getItem("gdata"+(i+5));
			obj1=JSON.parse(objt1);
			document.getElementById("hna"+i).innerHTML=obj1.gname;
			document.getElementById("hn"+i).innerHTML=obj1.gnum;
			document.getElementById("ht"+i).innerHTML=obj1.gdate.year+"-"+obj1.gdate.month+"-"+obj1.gdate.day + "-" + obj1.gdate.hours +":"+obj1.gdate.minutes +":"+obj1.gdate.seconds;
		}
	}
}